//index.js 获取到整个对象的事例
const app = getApp()
var common = require('../../utils/LogUtil.js')
Page({
  //参与页面渲染的数据（页面的初始化数据）
  data: {
    imgUrls: [],
    contextList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 1000,
    circular: true,
    scrollanimation: true,
    index: 0,
    isMore: true
  },
  //页面渲染后执行的方法（监听页面加载）
  onLoad: function() {
    var that = this
     
    wx.request({
      url: 'http://www.wanandroid.com/banner/json',
      method: 'GET',
      success(res) {
        that.setData({
          imgUrls: res.data.data
        })
      },
      fail() {
        console.log("请求得到的数据失败")
      }
    })
    this.getList()
  },
  /**
   * 上拉加载
   */
  onReachBottom: function() {
    this.data.index = this.data.index + 1
    this.getList()
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    this.data.index = 0
    this.data.contextList = []
    this.getList()
  },
  //页面显示
  onShow: function() {
    common.log("页面数据onShow")
  },
  //页面初次渲染完成
  onReady: function() {
    console.log("页面数据onReady")
  },
  onHide: function() {
    console.log("页面数据onHide")
  },
  onUnload: function() {
    console.log("页面数据onUnload")
  },

  /**
   * 获取列表中的数据
   */
  getList: function() {
    wx.showLoading({
      title: '正在加载中',
    })
    var that = this
    wx.request({
      url: 'http://www.wanandroid.com/article/list/' + that.data.index + '/ json',
      method: 'GET',
      success(res) {
        if (res.data.data.datas.length != 0 && that.data.isMore) {
          that.setData({
            contextList: that.data.contextList.concat(res.data.data.datas)
          })
        } else {
          that.data.isMore = false
        }

        wx.setStorage({
          key: "key",
          data: res.data.data.datas
        })

        wx.getStorage({
          key: 'key',
          success(res) {
            console.log(res.data)
          }
        })
        console.log(res.data)
        wx.hideLoading()
      },
      fail() {

      }
    })
  },

  /**
   * 点击查看详情内容
   */
  lookMessage: function(messgaePath) {
    getApp().data.globalData1++
      wx.navigateTo({
        url: '/pages/message/message?messagePath=' + messgaePath.currentTarget.dataset.path,
      })
  }
})